<template>
  <div>
    <!-- 使用isAdd 展示添加还是编辑 -->
    <el-dialog
      :title="info.isAdd?'活动添加':'活动编辑'"
      :visible.sync="info.isshow"
      @closed="closed"
      @opened="opened"
    >
      {{form}}
      <el-form :model="form">
        <!-- 2.绑定数据 -->
        <el-form-item label="活动名称" label-width="120px">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动期限" label-width="120px">
          <el-date-picker
            v-model="value1"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="一级分类" label-width="120px">
          <el-select placeholder="请选择" v-model="form.first_cateid" @change="changeFirstCateId">
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="二级分类" label-width="120px">
          <el-select placeholder="请选择" v-model="form.second_cateid" @change="changeSecondCateId">
            <el-option
              v-for="item in secondCateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品" label-width="120px">
          <el-select placeholder="请选择" v-model="form.goodsid">
            <el-option
              v-for="item in goodslist"
              :key="item.id"
              :label="item.goodsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  reqseckadd,
  reqseckedit,
  reqseckinfo,
  reqcatelist,
  reqgoodslist,
} from "../../../request/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { mapActions, mapGetters } from "vuex";
// import E from "wangeditor";
export default {
  computed: {
    ...mapGetters({
      cateList: "cate/list", //分类列表
      specsList: "specs/list", //规格列表
    }),
  },
  props: ["info"],
  data() {
    return {
      // 1.初始化数据
      form: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 2,
      },
      // 二级分类的列表
      secondCateList: [],
      value1: [],
      goodslist: [],
    };
  },

  mounted() {
    //3取分类数据
    if (this.cateList.length == 0) {
      this.reqCateList();
    }
    // 8请求规格列表
    this.reqSpecsList(true);
  },
  methods: {
    ...mapActions({
      reqCateList: "cate/reqList", //请求分类列表
      reqSpecsList: "specs/reqList",
      reqList: "seckill/reqList",
    }),
    opened() {
      // // 创建编辑器
      // this.editor = new E("#editor");
      // this.editor.create();
      // 赋值
      // this.editor.txt.html(this.form.description);
    },
    // 5.修改了1级分类ID
    changeFirstCateId() {
      //清空二级的数据
      this.form.second_cateid = "";
      //获取二级列表
      this.getSecondCateList();
    },
    getSecondCateList() {
      //根据1级 的id取二级分类的列表
      reqcatelist({
        pid: this.form.first_cateid,
      }).then((res) => {
        if (res.data.code == 200) {
          this.secondCateList = res.data.list ? res.data.list : [];
        }
      });
    },
    // 6.修改了2级分类ID
    changeSecondCateId() {
      this.form.goodsid = "";
      this.getGoodsList();
    },
    getGoodsList() {
      //根据1级 的id取二级分类的列表
      reqgoodslist({
        fid: this.form.first_cateid,
        sid: this.form.second_cateid,
      }).then((res) => {
        if (res.data.code == 200) {
          this.goodslist = res.data.list ? res.data.list : [];
        }
      });
    },
    // 9.修改了规格，计算规格属性
    changeSpecsId() {
      //清空规格属性的数据
      this.form.specsattr = [];
      this.getAttrsArr();
    },
    //计算规格属性列表
    getAttrsArr() {
      //将specsList 中id和form.specsid一致的数据attrs赋值给attrsArr
      let obj = this.specsList.find((item) => item.id == this.form.specsid);
      this.attrsArr = obj ? obj.attrs : []; //["s","m","l"]
    },
    sendFile(file) {
      this.form.img = file;
    },
    //新增规格属性
    addArr() {
      this.arr.push({
        value: "",
      });
    },
    //删除规格属性
    delArr(index) {
      this.arr.splice(index, 1);
    },

    //取消
    cancel() {
      this.info.isshow = false;
    },
    //弹框关闭
    closed() {
      if (!this.info.isAdd) {
        this.empty();
      }
      // this.form.description = this.editor.txt.html();
    },
    //重置form
    empty() {
      // 1.初始化数据
      this.form = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        goodsid: "",
        status: 2,
      };
      // 二级分类的列表
      this.secondCateList = [];
      //规格属性的列表
      this.attrsArr = [];
      this.value1 = [];
    },
    //验空的函数
    checkEmpty() {
      return new Promise((resolve, reject) => {
        if (this.form.first_cateid == "") {
          errorAlert("一级分类不能为空");
          return;
        }
        if (this.form.second_cateid == "") {
          errorAlert("二级分类不能为空");
          return;
        }
        resolve();
      });
    },
    //添加
    add() {
      //验空
      // this.checkEmpty().then(() => {
      // 数据交互
      reqseckadd({
        ...this.form,
        begintime: this.value1[0].getTime(),
        endtime: this.value1[1].getTime(),
      }).then((res) => {
        if (res.data.code == 200) {
          //成功了
          // （1）弹成功
          successAlert(res.data.msg);
          // (2)弹框消失
          this.cancel();
          // (3)清空数据
          this.empty();
          // (4)刷新列表
          this.reqList();
          // this.reqCount();
        }
      });
      // });
    },
    //获取一条数据
    getOne(id) {
      reqseckinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.value1.push(new Date(parseInt(this.form.begintime)));
          this.value1.push(new Date(parseInt(this.form.endtime)));
        }
      });
    },
    //修改
    update() {
      this.checkEmpty().then(() => {
        reqseckedit({
          ...this.form,
          begintime: this.value1[0].getTime(),
          endtime: this.value1[1].getTime(),
        }).then((res) => {
          if (res.data.code == 200) {
            // （1）弹成功
            successAlert(res.data.msg);
            // (2)弹框消失
            this.cancel();
            // (3)清空数据
            this.empty();
            // (4)刷新列表
            this.reqList();
          }
        });
      });
    },
  },
};
</script>
<style lang="less" scoped>
@import "../../../less/index.less";
.box {
  display: flex;
}
.box .el-input {
  flex: 1;
}
.box .el-button {
  width: auto;
}
</style>